package com.dong.pointviewpager.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.dong.pointviewpager.utils.DensityUtils.dp2px
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.dong.pointviewpager.config.PointViewConfig

/**
 * Created by Dong on 2018/3/16.
 */
class PointView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {

    var count = 0
    var percent = 0f
    var scrollType = 0

    private val config = PointViewConfig(context)

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (count != 0) {
            val nfPaint = Paint()
            nfPaint.isAntiAlias = false
            nfPaint.color = config.nfColor
            for (i in 0 until count) {
                canvas.drawCircle(
                    (config.distance + config.radius * 2) * i + config.distance / 2 + config.radius,
                    config.distance / 2 + config.radius,
                    config.radius,
                    nfPaint
                )
            }
            val fPaint = Paint()
            fPaint.isAntiAlias = false
            fPaint.color = config.fColor
            if (scrollType == INSTANT_SCROLL) percent = Math.round(percent).toFloat()
            canvas.drawCircle(
                (config.distance + config.radius * 2) * percent + config.distance / 2 + config.radius,
                config.distance / 2 + config.radius,
                config.radius,
                fPaint
            )
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        // 获取宽-测量规则大小
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        // 设置wrap_content的默认宽 / 高值
        val mWidth = ((config.distance + config.radius * 2) * count).toInt()
        val mHeight = (config.distance + config.radius * 2 + config.disbottom).toInt()

        // 当布局参数设置为wrap_content时，设置默认值
        if (layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(mWidth, mHeight)
        } else if (layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(mWidth, heightSize)
        } else if (layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            setMeasuredDimension(widthSize, mHeight)
        }
    }


    fun setNFColor(nfColor: Int) {
        config.nfColor = nfColor
    }

    fun setFColor(fColor: Int) {
        config.fColor = fColor
    }

    fun setDistance(distance: Float) {
        config.distance = dp2px(context, distance)
    }

    fun setRadius(radius: Float) {
        config.radius = dp2px(context, radius)
    }

    fun setDisBottom(disBottom: Float) {
        config.disbottom = dp2px(context, disBottom)
    }

    fun initialise() {
        val layoutParams = layoutParams as RelativeLayout.LayoutParams
        // 获取高-测量规则的模式和大小
        val mWidth = ((config.distance + config.radius * 2) * count).toInt()
        val mHeight = (config.distance + config.radius * 2 + config.disbottom).toInt()
        layoutParams.width = mWidth
        layoutParams.height = mHeight
        setLayoutParams(layoutParams)
    }

    companion object {
        const val SMOOTH_SCROLL = 0
        const val INSTANT_SCROLL = 1
    }
}